import { ApiProperty } from '@nestjs/swagger';
import { IsMobilePhone, ValidateIf, IsOptional, IsString, IsEnum } from 'class-validator';
import { Type } from 'class-transformer';//数据格式的转换

export class UserDto {
  // 提示有一个@ApiPropertyOptional()快捷方式装饰器，有助于避免连续输入 @ApiProperty({ required: false })。
  @ApiProperty({ required: false, description: '手机号码' })
  @IsMobilePhone('zh-CN', {}, { message: '手机号码格式错误' })
  @ValidateIf((o) => o.mobile != '')
  @IsOptional()
  readonly mobile?: string;

  @ApiProperty({ required: false, description: '用户名' })
  @IsString({ message: '用户名必须为字符类型' })
  @ValidateIf((o) => o.username != '')
  @IsOptional()
  readonly username?: string;

  @ApiProperty({ required: false, description: '密码' })
  @ValidateIf((o) => o.password != '')
  @IsOptional()
  readonly password?: string;

  @ApiProperty({ required: false, description: '状态', enum: [0, 1] })
  @IsEnum({ 禁用: 0, 当前可用: 1 }, { message: '状态必须是(0:表示禁止,1:表示正常)的数字' })
  @Type(() => Number)//把获取到的值转化为number
  @ValidateIf((o) => o.status != '')
  @IsOptional()
  readonly status?: number;

  @ApiProperty({ required: false, description: '角色', enum: [0, 1, 2] })
  @IsEnum({ 超级管理员: 0, 管理员: 1, 一般人员: 2 }, { message: '' })
  @Type(() => Number)
  @ValidateIf((o) => o.is_super != '')
  @IsOptional()
  readonly isSuper?: number;

  @ApiProperty({ required: false, description: '角色ids'})
  @ValidateIf((o) => o.roleIds != '')
  @IsOptional()
  readonly roleIds?: string;
  
}
